import torch
import torch.nn as nn
import torch.optim as optim
import matplotlib.pyplot as plt
from torch.utils.data import Dataset, DataLoader

# 定义数据获取类
class MyDataset(Dataset):
    def __init__(self, x_data, y_data):
        self.x_data = x_data
        self.y_data = y_data
    
    def __len__(self):
        return len(self.x_data)

    def __getitem__(self, idx):
        print(f"Getting item {idx}")
        x = torch.tensor(self.x_data[idx], dtype=torch.float32)
        y = torch.tensor(self.y_data[idx], dtype=torch.float32)
        return x, y

# 示例数据
x_data = [[1, 2], [3, 4], [5, 6], [7, 8]]
y_data = [1, 0, 1, 0]

dataset = MyDataset(x_data, y_data)

# 使用DataLoader加载数据
dataLoader = DataLoader(dataset, batch_size=2, shuffle=True)

for epoch in range(1):
    for batch_idx, (x, y) in enumerate(dataLoader):
        print(f"Epoch {epoch}, Batch {batch_idx}, x: {x}, y: {y}")